# -*- coding: utf-8 -*-
from random import uniform

   
class Model:
	""" This class implements a simpe 1st order markov model"""
    
	current_state = None
	initial_state = None
	emission_probability = None
	transition_probability = None
	start_probability = None

	
	def __init__(self, start_probability, transition_probability, emission_probability):
	
		if ( len(emission_probability.keys()) == len(start_probability.keys()) and len(emission_probability.keys()) == len(transition_probability.keys())):
			self.current_state = None
			self.states = transition_probability.keys()
			self.start_probability = start_probability
			self.transition_probability = transition_probability
			self.emission_probability = emission_probability
			
		
	def step(self):
		rnd = (uniform(1,100000))/100000.0
		if (self.current_state == None):
			self.current_state = self.__compute(self.start_probability, rnd)
			self.initial_state = self.current_state
		else:
			rnd = (uniform(1,100000))/100000.0
			self.current_state = self.__compute( self.transition_probability[self.current_state], rnd)
		rnd = (uniform(1,100000))/100000.0
		self.__emit(self.__compute(self.emission_probability[self.current_state], rnd))
		
		
		
	def __compute(self, ripartition, probability):
		sum = 0.0
		iter = ripartition.iterkeys()
		now = iter.next()
		while (ripartition[now]+sum < probability):
			sum += ripartition[now]
			now = iter.next()
		return now


	def __emit(self, emission):
		print emission
		
	

